package app.configuration {
    import app.debug.APPLog;
    import app.utils.InvokeArgumentManger;

    import copyengine.utils.CEStringUtils;
    import copyengine.utils.GeneralUtils;

    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.filesystem.File;
    import flash.net.URLLoader;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLRequest;

    public final class ConfigManger {
        private static var _instance:ConfigManger;

        public static function get instance():ConfigManger {
            if (_instance == null) {
                _instance = new ConfigManger();
            }
            return _instance;
        }


        /**
         *所有的FileNode节点
         */
        public var allNodeFileVector:Vector.<NodeFileMeta>;
        public var isUseATF:Boolean = false;

        private var mConfigFilePath:String;
        private var mConfigFileLoader:URLLoader;
        private var initFinishCallback:Function;

        public function ConfigManger() {
        }

        public function initialize(_argumentArray:Array, _finishCallback:Function):void {
            initFinishCallback = _finishCallback;

            if (InvokeArgumentManger.instance.isContainArgument("isUseATF")) {
                isUseATF = InvokeArgumentManger.instance.getInvokeArgument("isUseATF");
            }

            //如果参数指定了ConfigPath就用参数指定的,否则用默认的根目录下的"Config.json"
            // -configPath File:///E:/WorkGround/Config.json
            // -configPath File:////Users/eran/Documents/
            mConfigFilePath = InvokeArgumentManger.instance.isContainArgument("configPath") ? InvokeArgumentManger.instance.getInvokeArgument("configPath") : "Config.json";

            mConfigFileLoader = new URLLoader();
            mConfigFileLoader.load(new URLRequest(mConfigFilePath));
            mConfigFileLoader.dataFormat = URLLoaderDataFormat.TEXT;
            GeneralUtils.addTargetEventListener(mConfigFileLoader, Event.COMPLETE, onConfigFileLoaded);
            GeneralUtils.addTargetEventListener(mConfigFileLoader, IOErrorEvent.IO_ERROR, loadConfigFileOnError);
        }

        //=====================================//
        //===========LOAD CONFIG FILE=============//
        //=====================================//

        private function onConfigFileLoaded(e:Event):void {
            allNodeFileVector = new Vector.<NodeFileMeta>();

            var configObj:Object = JSON.parse(mConfigFileLoader.data);
            var fileFolderList:Array = configObj["fileFolderList"];
            for each (var fileObj:Object in fileFolderList) {
                var fileFolder:File = new File(fileObj["inputPath"]);
                var allSubFileArray:Array = fileFolder.getDirectoryListing();
                for each (var subFile:File in allSubFileArray) {
                    if (CEStringUtils.endsWith(subFile.name, ".swf")) {
                        var nodeFileMeta:NodeFileMeta = new NodeFileMeta();
                        nodeFileMeta.inputFile = subFile.nativePath;
                        nodeFileMeta.isUseATF = fileObj["isUseATF"] == null ? false : fileObj["isUseATF"];
                        nodeFileMeta.isShareTexture = fileObj["isShareTexture"];
                        nodeFileMeta.metaFileOutputPath = fileObj["metaFileOutputPath"];
                        nodeFileMeta.textureFileOutputPath = fileObj["textureOutputPath"];
                        nodeFileMeta.fileName = CEStringUtils.beforeLast(subFile.name, ".");
                        allNodeFileVector.push(nodeFileMeta);
                    }
                }
            }
            initFinishCallback();
        }

        private function loadConfigFileOnError(e:Event):void { APPLog.err("can not load" + mConfigFilePath + " file , check is file exist ?"); }


    }
}
